import { Controller, Query, Param } from '@nestjs/common';
import { Post, Body, Get } from '@nestjs/common';
import { RecordService } from './record.service';
import { CurrentUser } from '../auth/current-user.decorator';
import { CurrentUserType } from 'src/types/user';
import CreateRecordDto from './dto/create.record.dto';
import UpdateRecordDto from './dto/update.record.dto';
import { getFormatDate } from '../../utils';

@Controller('api')
export class RecordController {
  constructor(private readonly recordService: RecordService) {}

  @Post('addRecord')
  addRecord(
    @CurrentUser() user: CurrentUserType,
    @Body() data: CreateRecordDto,
    @Body('date') date: string,
  ) {
    if (!date) {
      date = getFormatDate();
    }
    return this.recordService.addRecord(user.id, data, date);
  }

  @Post('addRecords')
  addRecords(
    @CurrentUser() user: CurrentUserType,
    @Body('records') records: CreateRecordDto[],
    @Body('date') date: string,
  ) {
    if (!date) {
      date = getFormatDate();
    }
    return this.recordService.addRecords(user.id, records, date);
  }

  @Get('getRecords')
  getRecords(
    @CurrentUser() user: CurrentUserType,
    @Query('date') date: string,
  ) {
    if (!date) {
      date = getFormatDate();
    }
    return this.recordService.getDayData(user.id, date);
  }

  @Get('getRecords/:recordId')
  getMotion(@Param('recordId') id: string) {
    return this.recordService.getRecordById(id);
  }

  @Post('updateRecord')
  updateRecord(@Body() record: UpdateRecordDto) {
    return this.recordService.updateRecord(record);
  }

  @Post('deleteRecord')
  delete(@Body('recordId') recordId: string) {
    return this.recordService.deleteRecord(recordId);
  }

  @Get('test')
  test() {
    return 'Route works!';
  }
  
  @Get('getMonthStats')
  getMonthStats(
    @CurrentUser() user: CurrentUserType,
    @Query('month') month: string,
  ) {
    return this.recordService.getMonthStats(user.id, month);
  }
}
